﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CDStore3Tiers;

namespace CDStore3Tiers
{
    public class XLTaiKhoan
    {
        StoreCDDataContext db ;

        public XLTaiKhoan()
        {
            db = new StoreCDDataContext();
            db.Connection.ConnectionString = getConfig.getConnection();
        }
        public void updateTaiKhoan(DataGridView dgv)
        {
            if (dgv.Rows.Count > 0)
            {
                XuLyForm XL = new XuLyForm();
                dgv.EndEdit();
                foreach (DataGridViewRow r in dgv.Rows)
                {
                    string idTK = r.Cells["cnameID"].Value.ToString().Trim();
                    string passTK = r.Cells["cnamePassword"].Value.ToString().Trim();
                    string nvTK = r.Cells["cnameNV"].Value.ToString().Trim();
                    string cvTK = r.Cells["cnameCV"].Value.ToString().Trim();
                    int qhTK = int.Parse(r.Cells["cnameQH"].Value.ToString().Trim());

                    TaiKhoan PQ = db.TaiKhoans.Single(t => t.ID.Equals(idTK));
                    PQ.Password = passTK;
                    PQ.TenNV = nvTK;
                    PQ.ChucVu = cvTK;
                    PQ.QuyenHan = qhTK;

                    db.SubmitChanges();
                }
            }
        }
        public List<TaiKhoan> getBySoluong(int index)
        {
           // db = new StoreCDDataContext();
            List<TaiKhoan> listQH1 = new List<TaiKhoan>();
            List<TaiKhoan> listQH2 = new List<TaiKhoan>();
            List<TaiKhoan> listQH3 = new List<TaiKhoan>();
            List<TaiKhoan> list = new List<TaiKhoan>();

            foreach (TaiKhoan pq in db.TaiKhoans)
            {

                if (int.Parse(pq.QuyenHan.ToString()) == 1) listQH1.Add(pq);
                else if (int.Parse(pq.QuyenHan.ToString()) == 2) listQH2.Add(pq);
                else listQH3.Add(pq);
            }
            switch (index)
            {
                case 0:
                    list = db.TaiKhoans.ToList<TaiKhoan>();
                    break;
                case 1:
                    list = listQH1;
                    break;
                case 2:
                    list = listQH2;
                    break;
                case 3:
                    list = listQH3;
                    break;
            }
            return list;
        }
        public List<TaiKhoan> searchByID(List<TaiKhoan> PQ, string val)
        {
            val = val.ToLower();
            return PQ.Where(c => c.ID.ToLower().Contains(val) || c.TenNV.ToString().ToLower().Contains(val) || c.ChucVu.ToString().ToLower().Contains(val)).ToList<TaiKhoan>();
        }
        public List<ChucNang> LayDSTheoChucVu(string chucvu)
        {
          //  db = new StoreCDDataContext();
           // db.Connection.ConnectionString = getConfig.getConnection();
            List<ChucNang> cn = (from ds in db.ChucNangs
                                 where ds.QuyenHanMacDinh.Contains(chucvu)
                                 select ds).ToList<ChucNang>();
            return cn;

        }
        public void ThemQuyenHanMacDinh(string id, string chucvu)
        {
            List<ChucNang> listcn = LayDSTheoChucVu(chucvu);
            try
            {
                for (int i = 0; i < listcn.Count; i++)
                {
                    db.TaiKhoanChucNang_Insert(id, listcn[i].MaChucNang);
                }
            }
            catch
            {

            }
        }
    }
}
